package miiddleware

import (
	"class08/config"
	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v4"
	"net/http"
)

func Auth() gin.HandlerFunc {
	return func(c *gin.Context) {
		config.Init()
		tokenString := c.GetHeader("token")

		if tokenString == "" {
			c.JSON(http.StatusUnauthorized, gin.H{
				"error": "没有授权",
			})
			c.Abort()
			return
		}

		token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
			return []byte(config.Cfg.SecretKey), nil

		})
		if err != nil {
			c.JSON(http.StatusUnauthorized, gin.H{
				"error": err.Error(),
			})
			c.Abort()
			return
		}
		if !token.Valid {
			c.JSON(http.StatusUnauthorized, gin.H{
				"error": "Invaild token",
			})
			c.Abort()
			return
		}
		claims := token.Claims.(jwt.MapClaims)
		c.Set("userID", int(claims["id"].(float64)))
		c.Next()
	}
}
